import modelExtend from 'dva-model-extend'
import { querylist, remove } from 'services/transactionReceipt/transactionReceipt.js'
import * as customer from 'services/customer.js'
import { message } from 'antd'
import { pageModel } from './common'


export default modelExtend(pageModel, {
  namespace: 'transactionReceipt',
  state: {
    currentItem: {},
    modalVisible: false,
    editType: 'create',
    selectedRowKeys: [],
    searchModalVisable: false,
    customerInfo: {},
    customerRes: [],
    isfind: 'noSearch',
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/transactionreceipt/') {
          dispatch({ type: 'clearState' })
          const payload = location.query || { page: 1, status: 0 }
          dispatch({
            type: 'query',
            payload,
          })
        } else if (location.pathname === '/transactionAdminReceipt') {
          dispatch({ type: 'clearState' })
          const payload = location.query || { page: 1 }
          dispatch({
            type: 'query',
            payload,
          })
        }
      })
    },
  },

  effects: {
    * query ({ payload = {} }, { call, put }) {
      let sendData = { }
      Object.assign(sendData, payload)
      sendData.currentPage = payload.page
      const data = yield call(querylist, sendData)
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.data.data,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: data.data.total,
            },
          },
        })
      }
    },

    * delete ({ payload }, { call, put, select }) {
      const data = yield call(remove, { id: payload })
      const { selectedRowKeys } = yield select(_ => _.transactionReceipt)
      if (data.success) {
        message.success('delete success')
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
        yield put({ type: 'query' })
      } else {
        message.success('delete failed')
        throw data
      }
    },

    * multiDelete ({ payload }, { call, put }) {
      const data = yield call(remove, payload)
      if (data.success) {
        message.success('delete success')
        yield put({ type: 'updateState', payload: { selectedRowKeys: [] } })
        yield put({ type: 'query' })
      } else {
        message.success('delete failed')
        throw data
      }
    },

    * getCustomer ({ payload = {} }, { call, put }) {
      const customerRes = yield call(customer.listall, { ...payload })
      if (customerRes.data.length === 0) {
        yield put({
          type: 'updateState',
          payload: {
            isfind: false,
          },
        })
      }
      if (customerRes.data.length > 0) {
        yield put({
          type: 'updateState',
          payload: {
            customerRes: customerRes.data,
            isfind: true,
          },
        })
      }
    },
  },

  reducers: {

    updateSelectState (state, { payload }) {
      return { ...state, ...payload }
    },

    updateEditState (state, { payload }) {
      return { ...state, ...payload }
    },

    showSearchModal (state, { payload }) {
      return {
        ...state,
        searchModalVisable: true,
        isfind: 'noSearch',
        customerRes: [],
        customerInfo: {},
      }
    },

    closeSearchModal (state, { payload }) {
      return {
        ...state,
        searchModalVisable: false,
      }
    },
    clearState (state, { payload }) {
      return {
        ...state,
        currentItem: {},
        modalVisible: false,
        editType: 'create',
        selectedRowKeys: [],
        searchModalVisable: false,
        customerInfo: {},
        customerRes: [],
        isfind: 'noSearch',
      }
    },
  },
})
